﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.App;
using Domain.Types;
using DTO;
using FacadeApi.Extension;
using Infrastructure.Error;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace FacadeApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class TaskTakeController : ControllerBase
    {
        public TaskApplication taskApplication { get; set; }
        public ILogger<TaskTakeController> _logger { get; set; }
        /// <summary>
        /// 领取任务
        /// </summary>
        /// <param name="TaskID">FromForm</param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult TakeTask([FromForm] int TaskID)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = taskApplication.TakeTask(int.Parse(uid), TaskID);

                return this.Success(new { TakeTaskID=r.ID}, "领取成功");
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }
        /// <summary>
        /// 领取的任务提交资源审核
        /// </summary>
        /// <param name="submitTakeTask"></param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult SubmitTakeTask(SubmitTakeTaskDataDto submitTakeTask)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = taskApplication.SubmitTakeTask(int.Parse(uid), submitTakeTask);

                return this.Success(r, "提交成功");
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }



        [HttpPost, TokenValidFilter]
        public ActionResult TakeTaskAudit(TakeTaskAuditPassInput taskAuditPassInput)
        {
            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = taskApplication.TakeTaskAudit(int.Parse(uid), taskAuditPassInput);

                return this.Success(r, "提交成功");
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }

    }
}
